package com.hthy.futures.ui.fragment;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.hthy.futures.AppContext;
import com.hthy.futures.R;
import com.hthy.futures.api.ST_TradeApi;
import com.hthy.futures.base.BaseFragment;
import com.hthy.futures.bean.DeclareMoney;
import com.hthy.futures.utils.DialogUtil;
import com.hthy.futures.utils.ErrorUtils;
import com.hthy.futures.utils.LogUtils;
import com.hthy.futures.utils.ShaUtils;
import com.hthy.futures.utils.StringUtils;
import com.hthy.futures.view.EditTextWithDel2;
import com.vilyever.socketclient.SocketClient;

import net.hthy.bean.Trade;

import butterknife.Bind;

/**
 * 出金申请
 * Author:  Mr.bingley
 * Version:
 * Date:  2017/3/6
 */

// 簽約解約頁面

public class PushMoneyFragment extends BaseFragment {
    @Bind(R.id.login_user)
    TextView mLoginUser;
    @Bind(R.id.et_fund_outable_money)
    TextView mEtFundOutableMoney;
    @Bind(R.id.et_fund_pwd)
    EditTextWithDel2 mEtFundPwd;
    @Bind(R.id.et_fund_money)
    EditTextWithDel2 mEtFundMoney;
    @Bind(R.id.et_note_content)
    EditText mEtNoteContent;
    @Bind(R.id.bt_post)
    Button mBtPost;
    private String mUserName;
    private String mFundMoneyAble;
    private String mFundPwd;
    private String mFundMoney;
    private String mNoteContent;

    @Override
    protected int getLayoutId() {
        return R.layout.frag_push_money;
    }

    @Override
    protected void initWidget(View root) {
        super.initWidget(root);
        mLoginUser.setText(AppContext.accountName);
        mBtPost.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkValide()) {
                    return;
                }
                DeclareMoney declareMoney = new DeclareMoney();
                //  declareMoney.de_stop_money = mFundMoneyAble;   //
                declareMoney.de_userId = AppContext.accountId+"";
                declareMoney.de_fundPwd = ShaUtils.getSha1(mFundPwd);
                declareMoney.de_out_in_money = mFundMoney;
                declareMoney.de_operType = '2';

                socketClient.sendData(ST_TradeApi.moneyDeclaretrade(declareMoney));
                DialogUtil.showDialog(getActivity());
            }
        });

        // 用戶金額先從 資金接口中獲取
        socketClient.sendData(ST_TradeApi.searchGoods("2003"));
    }

    public boolean checkValide() {
        // mFundMoneyAble = mEtFundOutableMoney.getText().toString().trim();
        mFundPwd = mEtFundPwd.getText().toString().trim();
        mFundMoney = mEtFundMoney.getText().toString().trim();
        mNoteContent = mEtNoteContent.getText().toString().trim();
        /*if (StringUtils.isEmpty(mFundMoneyAble)) {
            showToast(R.string.able_money);
            return true;
        }*/
        if (StringUtils.isEmpty(mFundPwd)) {
            showToast(R.string.fundpwd);
            return true;
        }
        if (StringUtils.isEmpty(mFundMoney)) {
            showToast(R.string.fund_money);
            return true;
        }
        /*if (StringUtils.isEmpty(mNoteContent)) {
            showToast(R.string.note_content);
            return true;
        }*/
        return false;
    }


    @Override
    protected void onResponse(SocketClient client, String responMes, byte[] subBytes) {
        super.onResponse(client, responMes, subBytes);
        LogUtils.e("PushMoneyFragment", responMes);
        try {
            if (responMes.indexOf("2004") != -1) {

                double dbFreeMargin = Trade.QueryUserFundResponseMsg.parseFrom(subBytes).getUserFundDetail().getDbFreeMargin();
                mEtFundOutableMoney.setText(StringUtils.toDoubleStr(dbFreeMargin+""));

            } else if (responMes.indexOf("2018") != -1) {

                Trade.TransResponseMsg transResponseMsg = Trade.TransResponseMsg.parseFrom(subBytes);
                int iErrorCode = transResponseMsg.getResponseMsg().getIErrorCode();
                if (0 == iErrorCode) {
                    showToast(R.string.pushmoneysuccess);
                    getActivity().finish();
                } else {
                    ErrorUtils.sendErrorMes(getActivity(), iErrorCode + "");
                }
                DialogUtil.dimissDialog();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
